package com.yjhb.android.sbz.tool;

import android.Manifest;
import android.app.Activity;
import android.app.Dialog;

import com.yjhb.android.sbz.dialog.UpdateDialog;
import com.yjhb.android.sbz.entity.AppVersion;
import com.yjhb.android.sbz.mvp.appupdate.AppUpdatePresenter;
import com.jojo.android.zxlib.base.BaseDialog;
import com.jojo.android.zxlib.base.mvp.CommonView;
import com.jojo.android.zxlib.interfaces.Callback;
import com.jojo.android.zxlib.tool.Global;
import com.jojo.android.zxlib.tool.ZxAppTool;
import com.jojo.android.zxlib.tool.ZxDownloadTool;
import com.jojo.android.zxlib.tool.ZxFileTool;
import com.jojo.android.zxlib.tool.ZxToast;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.text.DecimalFormat;

import io.reactivex.functions.Consumer;

/**
 * 创建者 zx
 * 创建时间 2020/12/31 17:52
 */
public class AppUpdateTool implements CommonView<AppVersion> {
    private final String TAG = "AppUpdateTool";

    private AppUpdatePresenter appUpdatePresenter;
    private Activity activity;
    private Callback<Boolean> callback;

    public AppUpdateTool(Activity activity) {
        this.activity = activity;
        appUpdatePresenter = new AppUpdatePresenter(this);
    }

    public void check() {
        appUpdatePresenter.getAppLastVersion();
    }

    @Override
    public void onWait() {

    }

    @Override
    public void onSuccess(AppVersion data) {
        if (data != null) {
            if (data.getIncr() > ZxAppTool.getAppVersionCode(activity)) {
                UpdateDialog updateDialog = new UpdateDialog(activity);
                //强制更新
                if (data.getForceUpdate() == 1) {
                    updateDialog.setCancelable(false);
                    updateDialog.setCanceledOnTouchOutside(false);
                    updateDialog.showCloseButton(false);
                } else {
                    updateDialog.showCloseButton(true);
                }
                updateDialog.setOnClickListener(new BaseDialog.OnClickListener() {
                    @Override
                    public void onClick(Dialog dialog, int button) {
                        if (updateDialog.getState() == 0 || updateDialog.getState() == 2) {
                            if (updateDialog.getState() == 2) {
                                updateDialog.reset();
                            }
                            new RxPermissions(activity).request(Manifest.permission.WRITE_EXTERNAL_STORAGE,
                                    Manifest.permission.READ_EXTERNAL_STORAGE)
                                    .subscribe(new Consumer<Boolean>() {
                                        @Override
                                        public void accept(Boolean aBoolean) throws Exception {
                                            if (!aBoolean) {
                                                ZxToast.showFAILBottomToast("没有储存卡访问权限");
                                                return;
                                            }
                                            updateDialog.setState(1);
                                            ZxDownloadTool.DownloadFile(data.getUrl(), ZxFileTool.getAppSDFilePath() + "apk"
                                                    , new ZxDownloadTool.ProgressListener() {
                                                        @Override
                                                        public void onProgress(long totalBytes, long remainingBytes) {
                                                            Global.getInstance().getMainHandler().post(new Runnable() {
                                                                @Override
                                                                public void run() {
                                                                    DecimalFormat decimalFormat = new DecimalFormat("0.0%");
                                                                    updateDialog.setProgress((int) (((double) remainingBytes / totalBytes) * 100));
                                                                    updateDialog.setText(decimalFormat.format((double) remainingBytes / totalBytes));
                                                                }
                                                            });

                                                        }

                                                        @Override
                                                        public void onFailure(String msg) {
                                                            Global.getInstance().getMainHandler().post(new Runnable() {
                                                                @Override
                                                                public void run() {
                                                                    ZxToast.showFAILBottomToast("更新失败了QAQ~~");
                                                                    updateDialog.setState(0);
                                                                    updateDialog.setProgress(0);
                                                                    updateDialog.setText("重试");
                                                                }
                                                            });

                                                        }

                                                        @Override
                                                        public void onComplete(String filePath) {
                                                            Global.getInstance().getMainHandler().post(new Runnable() {
                                                                @Override
                                                                public void run() {
                                                                    updateDialog.setState(2);
                                                                    updateDialog.setText("更新");
                                                                    ZxAppTool.installApp(activity, filePath);
                                                                }
                                                            });
                                                        }
                                                    });
                                        }
                                    });
                        }

                    }
                });
                updateDialog.setVersionName(data.getVersion());
                updateDialog.setVersionDepict(data.getContent());
                updateDialog.show();

            } else {
                if (callback != null) {
                    callback.callback(false);
                }
            }
        } else {
            if (callback != null) {
                callback.callback(false);
            }
        }
    }

    @Override
    public void onFailure(int code, String msg) {

    }

    public void setCallback(Callback<Boolean> callback) {
        this.callback = callback;
    }
}
